View Javadoc
1 /* 2 * Copyright (c) 2001 by 3 * Siegfried GOESCHL <mailto:siegfried.goeschl@itserv.at>; 4 * and Dima STADNIK <mailto:5d5@mail.ru>; 5 * 6 * This program is free software. 7 * 8 * You may redistribute it and/or modify it under the terms of the GNU 9 * General Public License as published by the Free Software Foundation. 10 * Version 2 of the license should be included with this distribution in 11 * the file LICENSE, as well as License.html. If the license is not 12 * included with this distribution, you may find a copy at the FSF web 13 * site at 'www.gnu.org' or 'www.fsf.org', or you may write to the 14 * Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA. 15 * 16 * THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND, 17 * NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR 18 * OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY 19 * CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR 20 * REDISTRIBUTION OF THIS SOFTWARE. 21 */ 22 23 package junit.extensions; 24 25 import java.util.ArrayList; 26 import java.io.PrintStream; 27 import junit.framework.Test; 28 import junit.framework.TestResult; 29 import junit.textui.TestRunner; 30 import junit.textui.ResultPrinter; 31 32 33 /*** 34 * Extended version of junit.textui.TestRunner. 35 * <p>New features: 36 * <ul> 37 * <li>Verbose mode (-v) 38 * <li>Repeating a test suite multiple times (-r n) 39 * <li>Executing a test suite with multiple threads simultanously (-s n) 40 * <li>Executing a test suite with delay between test cases (-w ms) 41 * <li>Help is also available (-h) 42 * </ul> 43 * 44 * @author Siegfried GOESCHL 45 */ 46 47 public class PPTestRunner extends TestRunner { 48 49 /*** a copy of the instance variable declared private by TestRunner */ 50 private ResultPrinter fPrinter; 51 52 /*** enable verbose output */ 53 private boolean verbose = false; 54 55 /*** number of time to rerun the test */ 56 private int repetitions = 0; 57 58 /*** number of threads running the tests */ 59 private int stress = 0; 60 61 /*** delay in ms to wait between two test cases */ 62 private int delay = 0; 63 64 /*** name of the single test case to execute */ 65 private String testCaseName = null; 66 67 /*** 68 * Constructs a PPTestRunner. 69 */ 70 71 public PPTestRunner() { 72 // taken from TestRunner 73 this(System.out); 74 } 75 76 /*** 77 * Constructs a PPTestRunner using the given stream for all the output 78 */ 79 80 public PPTestRunner(PrintStream writer) { 81 // taken from TestRunner 82 this(new ResultPrinter(writer)); 83 } 84 85 /*** 86 * Constructs a PPTestRunner using the given ResultPrinter all the output 87 */ 88 89 public PPTestRunner(ResultPrinter printer) { 90 super(printer); 91 this.fPrinter = printer; 92 } 93 94 /*** 95 * Creates the TestResult to be used for the test run. 96 */ 97 protected TestResult createTestResult() { 98 return new PPTestResult( 99 this.fPrinter.getWriter(), this.delay, this.verbose, this.testCaseName); 100 } 101 102 public static void main(String args[]) { 103 int result = 0; 104 105 if (args.length == 0 || args[0].equals("-?") || args[0].equalsIgnoreCase("-h")) { 106 showHelp(); 107 } else { 108 try { 109 PPTestRunner testRunner = new PPTestRunner(); 110 TestResult testResult = testRunner.start(args); 111 112 if (!testResult.wasSuccessful()) { 113 System.exit(FAILURE_EXIT); 114 } else { 115 System.exit(SUCCESS_EXIT); 116 } 117 } catch (Exception e) { 118 System.err.println(e.getMessage()); 119 System.exit(EXCEPTION_EXIT); 120 } 121 } 122 } 123 124 /*** 125 * Initializes JUNITPP with the provided command line arguments and 126 * and starts the test run 127 * @param args command line parameters for initializing JUNITPP 128 */ 129 130 protected TestResult start(String args[]) throws Exception { 131 ArrayList argsList = new ArrayList(); 132 133 // extract all options known to JUNITPP and run the baseclass 134 // with the remaining options as suggested by Dave Grays 135 136 for (int i = 0; i < args.length; i++) { 137 if (args[i].equals("-v")) 138 verbose = true; 139 else if (args[i].equals("-r")) 140 repetitions = Integer.parseInt(args[++i]); 141 else if (args[i].equals("-s")) 142 stress = Integer.parseInt(args[++i]); 143 else if (args[i].equals("-w")) 144 delay = Integer.parseInt(args[++i]); 145 else if (args[i].equals("-t")) 146 testCaseName = args[++i]; 147 else { 148 argsList.add(args[i]); 149 } 150 } 151 152 String[] junitArgs = 153 (String[]) argsList.toArray(new String[argsList.size()]); 154 155 return super.start(junitArgs); 156 } 157 158 public TestResult doRun(Test test, boolean wait) { 159 Test targetTest = test; 160 161 if (repetitions > 1) { 162 targetTest = new RepeatedTest(targetTest, repetitions); 163 } 164 if (stress > 1) { 165 targetTest = new ActiveTest(targetTest, stress); 166 } 167 return super.doRun(targetTest, wait); 168 } 169 170 /*** Prints program help. */ 171 protected static void showHelp() { 172 System.err.println("Parameters: [-wait] [-v] [-r n] [-s m] [-t name] [-w ms] TestSuiteName"); 173 System.err.println("\tTestSuiteName is the name of the test suite to invoke"); 174 System.err.println("\tw ms is the number of milliseconds between invocations of test cases"); 175 System.err.println("\ts m is number of execution threads"); 176 System.err.println("\tr n is the number of repititions"); 177 System.err.println("\tt name is the test case to execute"); 178 System.err.println("\tv verbose mode"); 179 System.err.println(""); 180 System.err.println("System Properties :"); 181 System.err.println("\tjunit.conf - configuration file name or search path"); 182 System.err.println("\tjunit.conf.class - class name of configuration implementation"); 183 System.err.println("\tjunit.parameters - command line counterpart"); 184 System.err.println(""); 185 System.err.println("Examples :"); 186 System.err.println("\tjava -cp junit.jar junit.extensions.PPTestRunner junit.samples.VectorTest"); 187 System.err.println("\tjava -cp junit.jar junit.extensions.PPTestRunner -r 2 -s 2 -w 10 -v junit.samples.VectorTest"); 188 System.err.println("\tjava -cp junit.jar -Djunit.conf=. junit.extensions.PPTestRunner junit.tests.ConfigurableTestCaseTest"); 189 System.err.println("\tjava -cp junit.jar -Djunit.conf=. -Djunit.parameters=\"-C client.conf\" junit.extensions.PPTestRunner MyTestCase"); 190 System.err.println(""); 191 } 192 }

This page was automatically generated by Maven